<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="请输入员工名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.phone" placeholder="请输入手机号" clearable></el-input>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-select v-model="filters.state" placeholder="请选择" clearable>
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="queryDepts">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd()">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleExport">导出</el-button>
        </el-form-item>
        <el-form-item label="" prop="resource">
          <el-col :span="12">
            <el-upload
                class="upload-demo"
                action="http://localhost:8080/emp/importExcel"
                list-type="picture">
              <el-button  type="primary">点击导入</el-button>
            </el-upload>
          </el-col>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="depts" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="员工姓名" width="120" sortable>
      </el-table-column>
      <el-table-column prop="phone" label="员工手机号" width="130" sortable>   <!--:formatter="formatSex" 性别-->
      </el-table-column>
      <el-table-column prop="state" label="是否启用" width="130"  sortable>
        <template scope="scope"><!-- scope作用 就是用来获取当前行的值 -->
          <!-- v-if v-show-->
          <div v-if="scope.row.state == 0"  slot="reference" >
            <el-tag size="medium" type="info">禁用</el-tag>
          </div>
          <div v-if="scope.row.state == 1" slot="reference" >
            <el-tag size="medium" type="success">启用</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="salt" label="登录账号" width="120" sortable>
      </el-table-column>
      <el-table-column prop="password" label="登录密码" min-width="120" sortable>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handlesave(scope.$index, scope.row,'修改')">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>

<!--  分页      -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="float:right;">
      </el-pagination>

    </el-col>

    <!--添加.编辑.保存界面-->
    <!--
      :visible.sync 控制模态框是否显示
      :rules="saveFormRules"  表单验证的绑定
      prop="sn" 专门用来做表单验证

    -->
    <el-dialog :title="saveTitle" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="员工名称" prop="username">
          <el-input v-model="saveForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工手机号" prop="phone">
          <el-input v-model="saveForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="登录账号" prop="salt">
          <el-input v-model="saveForm.salt" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="登录密码" prop="password">
          <el-input v-model="saveForm.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">启用</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
export default {
  data() {
    return {
      filters: { //高级查询
        keyword: '',
        phone: '',
        state:'',
      },
      depts: [],
      total: 0,
      currentPage:1,
      pageSize:10,
      listLoading: false,

      //批量删除使用的
      sels: [],//列表选中列

      //新增，或者修改用
      saveFormVisible: false,//新增界面是否显示
      saveTitle:"",//设置标题  新增或者是编辑
      saveLoading: false,//忙等
      saveFormRules: { //表单的验证
        username: [
          //   必传         提示信息                失去焦点触发
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        phone : [
          { required: true, message: '请输入员工手机号', trigger: 'blur' }
        ],
        salt : [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        password : [
          { required: true, message: '请输入登录密码', trigger: 'blur' }
        ]
      },

      employees:[],//存放员工列表的数据
      //编辑和新增界面数据  弹出框的form标单
      saveForm: {
        id: null,
        username:"",
        email: '',
        phone: '',
        salt: '',
        password: '',
        age: null,
        state: 1,
        department_id: null,
        logininfo_id: null,
        shop_id: null,
      },
    }
  },
  methods: {
    //当前页的事件
    handleCurrentChange(val) { //自动获取当前页码
      this.currentPage = val;
      this.getdepts(); //刷新列表
    },
    //选择条数的事件
    handleSizeChange(val){//自动获取当前页的条数
      this.pageSize=val
      this.getdepts();
    },
    queryDepts(){
      this.currentPage = 1
      this.getdepts()
    },
    //获取用户列表
    getdepts() {  //这里是发送请求
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.filters.keyword,
        sn: this.filters.sn,
        state: this.filters.state,
      };
      this.listLoading = true;//开启忙等
      //获取部门的分页参数
      this.$http.post("/emp/pageList",para).then(res=>{
        this.depts=res.data.data
        this.total=res.data.total
        this.listLoading = false; //加载完就关闭忙等
      })
      //NProgress.start();
      /*getdeptListPage(para).then((res) => {
        this.total = res.data.total;
        this.depts = res.data.depts;
        this.listLoading = false;
        //NProgress.done();
      });*/
    },
    //这个用来获取管理员的
    //单独获取Employee 里面的数据
    getEmployee(){
      this.$http.get("/emp/list").then(res=>{
        this.employees = res.data
      })
    },

    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'  //设置图标类型
      }).then(() => { //这是点了确定要做的事
        this.listLoading = true; //开启忙等
        //发送请求
        //"/dept/"+row.id 因为后端使用的{id} 所以用row 把后端的数据传的到前端
        this.$http.delete("/emp/"+row.id).then(res=>{
          this.listLoading = false; //数据加载出来了，就关闭忙等
          //做判断，是否删除成功 跟批量删除是一样的
          //如果是成功的就执行成功弹框 ,然后并刷新列表
          if(res.data.success){
            this.$message({
              message: res.data.msg,
              type: 'success'
            })
            this.currentPage = 1 //删除后回到第一页
            this.getdepts(); //删除成功一定要刷新列表
          }else { //是失败执行失败弹框
            this.$message({
              message: res.data.msg,
              type: 'error'
            })
          }
        })
      }).catch(() => {//取消要做的事情
      });
    },

    //显示编辑界面
    handlesave: function (index, row,title) {
      if(this.$refs['editForm']){
        this.$refs['editForm'].resetFields();//重置表单验证
      }
      this.saveTitle=title
      this.saveFormVisible = true; //是否显示
      this.saveForm = Object.assign({}, row); //给表单赋值
    },
    //导出
    handleExport(){
      location.href="http://localhost:8080/emp/export";
    },
    //显示新增界面
    handleAdd: function () {
      this.saveTitle= "新增"
      this.saveForm = {
        id: null,
        username:"",
        email: '',
        phone: '',
        salt: '',
        password: '',
        age: null,
        state: 1,
        department_id: null,
        logininfo_id: null,
        shop_id: null,
      }
      this.saveFormVisible = true;
    },
    //编辑
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => { //手动触发验证规则
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true; //开启忙等
            //因为双向绑定
            let para = Object.assign({}, this.saveForm); //克隆

            //发送请求
            this.$http.put("/emp",para).then(res=>{
              //判断，如果成功的话就弹出提示框
              if(res.data.success){
                this.saveLoading = false; //成功就关闭忙等
                this.$message({
                  message: res.data.msg,
                  type: 'success'
                });
                //重置表单验证
                this.$refs['saveForm'].resetFields();
                this.saveFormVisible = false;
                this.getdepts();
              }else {
                this.$message({
                  message: res.data.msg,
                  type: 'error'
                })
                this.saveLoading = false;
              }
            })

            });
        }
      });
    },

    selsChange: function (sels) {//给多选框绑定事件，如果勾选就会获取当前的值
      this.sels = sels; //将选择器中的数据赋值给this.sels
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);  //返回一个ids数组，千万不要加tostring 不然返回的是字符串
      console.log(ids)
      this.$confirm('确认删除选中记录吗？', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;  //开启忙等
        console.log(ids)
        //NProgress.start();
        this.$http.patch("/emp/del",ids).then(res=>{
          this.listLoading = false; //如果加载进去就关闭忙等
          //做判断，如果他成功了，就弹出提示框删除成功，或者就是失败
          if (res.data.success){
            //成功就弹出提示框 并刷新列表
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
            this.getdepts();//刷新列表
          }else {  //失败也弹出提示框 提醒删除失败 删除失败就不需要刷新列表，保持原数据
            this.$message({
              message: res.data.msg,
              type: 'error'
            })
          }
        })
      }).catch(() => { //这是点取消后做的事，点取消现在不需要做任何事

      });
    }
  },
  mounted() {
    this.getdepts();
    this.getEmployee();
  }
}

</script>

<style scoped>

</style>
